// v-debounce 指令：用于按钮、输入框等事件防抖
import { MessagePlugin } from 'tdesign-vue-next';

const debounceTips = [
  '手速太快啦，稍等一下！',
  '操作收到，稍作休息~',
  '你点得太快啦，系统喘口气~'
];

export default {
  mounted(el, binding) {
    let timer = null;
    const delay = binding.arg ? parseInt(binding.arg) : 400;
    const event = binding.modifiers.input ? 'input' : 'click';
    const handler = binding.value;
    // 随机选一句
    const getTip = () => handler && handler.__debounceTip__ || debounceTips[Math.floor(Math.random() * debounceTips.length)];
    if (typeof handler !== 'function') return;
    el.__debounceHandler__ = function (...args) {
      if (timer) clearTimeout(timer);
      timer = setTimeout(() => {
        handler.apply(this, args);
        MessagePlugin.info(getTip()).then(r => {});
      }, delay);
    };
    el.addEventListener(event, el.__debounceHandler__);
  },
  beforeUnmount(el, binding) {
    const event = binding.modifiers.input ? 'input' : 'click';
    el.removeEventListener(event, el.__debounceHandler__);
    delete el.__debounceHandler__;
  }
}; 